define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'schoolclass/index' + location.search,
                    add_url: 'schoolclass/add',
                    edit_url: 'schoolclass/edit',
                    del_url: 'schoolclass/del',
                    multi_url: 'schoolclass/multi',
                    import_url: 'schoolclass/import',
                    table: 'schoolclass',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'school.name', title: __('School_id')},
                        {field: 'grade.gradeName', title: __('Grade_id')},
                        {field: 'grade_class.name', title: __('Class_id')},
                        {field: 'teachers.nickname', title: __('Teacher_id')},
                        {field: 'student_num', title: __('班级人数')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });


            $(".btn-fix").click(function () {
                $.post("schoolclass/fix", function (data) {
                    location.reload();
                });
            });


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $("#c-grade_id").change( function (obj) {
                //obj为SelectPage对象
                $("#c-class_id").val("");
            });

            $("#c-class_id").data("params", function (obj) {
                return { custom: { grade_id: ['in',$("#c-grade_id").val() ]} };
            });

            $("#c-teacher_id").data("params", function (obj) {
                return { school_id: $("#c-school_id").val() };
            });

            Controller.api.bindevent();
        },
        edit: function () {
            $("#c-grade_id").change( function (obj) {
                //obj为SelectPage对象
                $("#c-class_id").val("");
            });

            $("#c-class_id").data("params", function (obj) {
                return { custom: { grade_id: ['in',$("#c-grade_id").val() ]} };
            });
            $("#c-teacher_id").data("params", function (obj) {
                return { school_id: $("#c-school_id").val() };
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
